#!/bin/sh

HOSTS_FILE=/etc/hosts
IP_ADDRESS="127.0.0.1"
HOST_NAME="technology-config.cn"

# 检查配置是否存在
if grep -q "${HOST_NAME}" "${HOSTS_FILE}"; then
  CURRENT_IP=$(grep "${HOST_NAME}" "${HOSTS_FILE}" | awk '{print $1}')

  if [ "${CURRENT_IP}" != "${IP_ADDRESS}" ]; then
    sed -i -e "/${HOST_NAME}/s/.*/${IP_ADDRESS}    ${HOST_NAME}/" "${HOSTS_FILE}"
    echo "IP address updated for ${HOST_NAME} in ${HOSTS_FILE}"
  else
    echo "IP address for ${HOST_NAME} is already up to date in ${HOSTS_FILE}"
  fi
else
  echo "${IP_ADDRESS}    ${HOST_NAME}" >> "${HOSTS_FILE}"
  echo "Host ${HOST_NAME} added to ${HOSTS_FILE}"
fi